/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.tendon.convert.json.streams;

import com.tencent.tendon.convert.json.*;
import java.io.IOException;

/**
 *
 * @author nbzhang
 */
public final class JsonStreamWriter extends JsonAbstractWriter {

    private final Appendable out;

    public JsonStreamWriter(Appendable out) {
        this.out = out;
    }

    @Override
    public void write(char ch) {
        try {
            out.append(ch);
        } catch (IOException ex) {
            throw new JsonException(ex);
        }
    }

    @Override
    public void write(char... chs) {
        try {
            for (char ch : chs) {
                out.append(ch);
            }
        } catch (IOException ex) {
            throw new JsonException(ex);
        }
    }

    @Override
    public void write(final char[] chs, final int start, final int end) {
        try {
            for (int i = start; i <= end; i++) {
                out.append(chs[i]);
            }
        } catch (IOException ex) {
            throw new JsonException(ex);
        }
    }

    @Override
    public void write(boolean quote, String value) {
        try {
            if (quote) {
                out.append('"');
                out.append(value);
                out.append('"');
            } else {
                out.append(value);
            }
        } catch (IOException ex) {
            throw new JsonException(ex);
        }
    }
}
